home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / utils / Randomizer.as < prev    next >
Text File  |  2008-11-04  |  1KB  |  64 lines

  1. package com.jeroenwijering.utils
  2. {
  3.    public class Randomizer
  4.    {
  5.        
  6.       
  7.       private var todo:Array;
  8.       
  9.       private var done:Array;
  10.       
  11.       private var original:Array;
  12.       
  13.       public function Randomizer(param1:Number)
  14.       {
  15.          var _loc2_:Number = NaN;
  16.          super();
  17.          original = new Array();
  18.          todo = new Array();
  19.          done = new Array();
  20.          _loc2_ = 0;
  21.          while(_loc2_ < param1)
  22.          {
  23.             original.push(_loc2_);
  24.             _loc2_++;
  25.          }
  26.       }
  27.       
  28.       public function pick() : Number
  29.       {
  30.          var _loc1_:Number = NaN;
  31.          var _loc2_:Number = NaN;
  32.          var _loc3_:Number = NaN;
  33.          if(todo.length == 0)
  34.          {
  35.             _loc3_ = 0;
  36.             while(_loc3_ < original.length)
  37.             {
  38.                todo.push(_loc3_);
  39.                _loc3_++;
  40.             }
  41.          }
  42.          _loc1_ = Math.floor(Math.random() * todo.length);
  43.          _loc2_ = todo[_loc1_];
  44.          done.push(todo.splice(_loc1_,1)[0]);
  45.          return _loc2_;
  46.       }
  47.       
  48.       public function get length() : Number
  49.       {
  50.          return todo.length;
  51.       }
  52.       
  53.       public function back() : Number
  54.       {
  55.          if(done.length < 2)
  56.          {
  57.             return pick();
  58.          }
  59.          todo.push(done.pop());
  60.          return done[done.length - 1];
  61.       }
  62.    }
  63. }
  64.